package com.springboot.lock.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author: TongRui乀
 * @Date: 2019/1/15 9:42
 * @description： 自定义配置RedisTemplate
 */
@Configuration
public class RedisTemplateJedisConfig {

    @Autowired
    private Environment environment;

    @Bean
    public RedisTemplate<String, Object> redisCacheTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(jedisConnectionFactory);
        return template;
    }


    @Bean
    @Primary
    public JedisConnectionFactory jedisConnectionFactory(){

        RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();

        standaloneConfiguration.setHostName(environment.getProperty("spring.redis.host"));
        standaloneConfiguration.setPassword(RedisPassword.of(environment.getProperty("spring.redis.password").toCharArray()));
        standaloneConfiguration.setPort(environment.getProperty("spring.redis.port", Integer.class));
        standaloneConfiguration.setDatabase(0);

        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpb=
                (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(environment.getProperty("spring.redis.jedis.pool.max-idle", Integer.class));
        jedisPoolConfig.setMinIdle(environment.getProperty("spring.redis.jedis.pool.min-idle", Integer.class));
        jedisPoolConfig.setMaxTotal(environment.getProperty("spring.redis.jedis.pool.max-active", Integer.class));
        String maxWait = environment.getProperty("spring.redis.jedis.pool.max-wait");
        int l=Integer.parseInt(maxWait.substring(0,maxWait.length()-2));
        jedisPoolConfig.setMaxWaitMillis(l);
        jpb.poolConfig(jedisPoolConfig);
        JedisConnectionFactory jedisConnectionFactory=new JedisConnectionFactory(standaloneConfiguration,jpb.build());

        return jedisConnectionFactory;
    }

}
